﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;

public partial class Templates_Hierarchy : BaseTemplatePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        dsChildren.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<TemplateHierarchy>>(dsChildren_SelectData);
        dsTemplates.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Template>>(dsTemplates_SelectData);
        btnAdd.Click += new EventHandler(btnAdd_Click);
        btnCancel.Click += new EventHandler(btnCancel_Click);
        btnSave.Click += new EventHandler(btnSave_Click);
        gridChildren.ItemInserted += new Telerik.Web.UI.GridInsertedEventHandler(gridChildren_ItemInserted);
        dsChildren.UpdatingData += new EventHandler<Acms.DataControls.UpdatingDataEventArgs<TemplateHierarchy>>(dsChildren_UpdatingData);
        gridChildren.ItemCommand += new Telerik.Web.UI.GridCommandEventHandler(gridChildren_ItemCommand);
        if (!IsPostBack)
        {
            this.Title = string.Format("{0} Template Hierarchy", base.CurrentTemplate.Name);
        }
    }

    void gridChildren_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == "MoveUp" || e.CommandName == "MoveDown")
        {
            int change = 1;
            if (e.CommandName == "MoveUp")
            {
                change = -1;
            }
            List<TemplateHierarchy> tmp = TemplateHierarchy.GetAllByParentId(TemplateId);
            TemplateHierarchy.Move(tmp, e.Item.DataSetIndex, e.Item.DataSetIndex + change);
            gridChildren.Rebind();
            gridChildren.SelectedIndexes.Clear();
            gridChildren.SelectedIndexes.Add(e.Item.ItemIndex + change);
        }
    }

    void dsChildren_UpdatingData(object sender, Acms.DataControls.UpdatingDataEventArgs<TemplateHierarchy> e)
    {
        e.Item.ParentId = TemplateId;
    }

    void gridChildren_ItemInserted(object source, Telerik.Web.UI.GridInsertedEventArgs e)
    {
        if (e.Exception == null || e.ExceptionHandled)
        {
            btnAdd.Visible = true;
            btnSave.Visible = btnCancel.Visible = false;
        }
    }

    void btnSave_Click(object sender, EventArgs e)
    {
        gridChildren.MasterTableView.PerformInsert();
    }

    void btnCancel_Click(object sender, EventArgs e)
    {
        btnAdd.Visible = true;
        btnSave.Visible = btnCancel.Visible = false;

        gridChildren.MasterTableView.IsItemInserted = false;
        gridChildren.Rebind();
    }

    void btnAdd_Click(object sender, EventArgs e)
    {
        btnAdd.Visible = false;
        btnSave.Visible = btnCancel.Visible = true;
        
        gridChildren.MasterTableView.IsItemInserted = true;
        gridChildren.Rebind();
    }

    void dsTemplates_SelectData(object sender, Acms.DataControls.SelectEventArgs<Template> e)
    {
        List<Template> templates = Template.GetAllBySiteId(AcmsContext.SiteId);
        List<TemplateHierarchy> existingChildren = TemplateHierarchy.GetAllByParentId(TemplateId);
        List<Template> data = new List<Template>();
        foreach (Template template in templates)
        {
            bool used = false;
            foreach (TemplateHierarchy child in existingChildren)
            {
                if (template.Id == child.ChildId)
                {
                    used = true;
                    break;
                }
            }
            if (!used)
            {
                data.Add(template);
            }
        }
        e.SetData(data);
    }

    void dsChildren_SelectData(object sender, Acms.DataControls.SelectEventArgs<TemplateHierarchy> e)
    {
        e.SetData(TemplateHierarchy.GetAllByParentId(TemplateId));
    }

    public override void GetBreadCrumbs(BaseMasterPage.BreadCrumbCollection breadcrumbs)
    {
        base.GetBreadCrumbs(breadcrumbs);
        breadcrumbs.Add("Hierarchy");
    }

}
